#include <qstring.h>
#include <qregexp.h>
#include <qstringlist.h>

#include <mythtv/mythcontext.h>
#include <mythtv/mythdbcon.h>

#include "dbcheck.h"
#include "programmedlg.h"

namespace
{
  const QString currentDatabaseVersion = "1000";
  const QString MythiPlayerVersionName = "mythiplayer.DBSchemaVer";

  void UpdateDBVersionNumber(const QString &field_name,
                              const QString &newnumber)
  {
      MSqlQuery query(MSqlQuery::InitCon());

      query.exec(QString("DELETE FROM settings WHERE value='%1';")
                  .arg(field_name));
      query.exec(QString("INSERT INTO settings (value, data, hostname) "
                          "VALUES ('%1', %2, NULL);")
                  .arg(field_name).arg(newnumber));
  }

  void performActualUpdate(const QStringList &updates, const QString &version,
                            QString &dbver, const QString &field_name)
  {
      MSqlQuery query(MSqlQuery::InitCon());

      VERBOSE(VB_IMPORTANT,
              QString("Upgrading to MythiPlayer schema version %1")
              .arg(version));

      for (QStringList::const_iterator p = updates.begin();
            p != updates.end(); ++p)
      {
          query.exec(*p);
      }

      UpdateDBVersionNumber(field_name, version);
      dbver = version;
  }

  void performActualUpdate(const QString updates[], const QString &version,
                            QString &dbver, const QString &field_name)
  {
      QStringList upQuery;
      for (int i = 0; ; ++i)
      {
          QString q = updates[i];
          if (q == "") break;
          upQuery.append(q);
      }
      performActualUpdate(upQuery, version, dbver, field_name);
  }

  void InitializeProgrammeDatabase()
  {
      VERBOSE(VB_IMPORTANT,
              "Inserting MythiPlayer initial database information.");

      const QString updates[] = {
    "CREATE TABLE IF NOT EXISTS `iplayer_genre` ("
    "  `intid` int(10) unsigned NOT NULL auto_increment,"
    "  `genre` varchar(128) NOT NULL,"
    "  PRIMARY KEY  (`intid`)"
    ");",

    "CREATE TABLE IF NOT EXISTS `iplayer_metadata` ("
    "  `intid` int(11) NOT NULL auto_increment,"
    "  `title` varchar(128) NOT NULL,"
    "  `description` text NOT NULL,"
    "  `series` varchar(128) NOT NULL,"
    "  `rating` varchar(128) NOT NULL,"
    "  `userrating` float NOT NULL,"
    "  `showlevel` int(11) NOT NULL,"
    "  `filename` text NOT NULL,"
    "  `coverfile` varchar(255) NOT NULL,"
    "  `inetref` varchar(255) NOT NULL,"
    "  `playcommand` varchar(255) default NULL,"
    "  `url` text NOT NULL,"
    "  `srcFile` varchar(150) NOT NULL,"
    "  `updated` int(11) NOT NULL,"
    "  PRIMARY KEY  (`intid`)"
    ");",

    "    CREATE TABLE IF NOT EXISTS `iplayer_metadatagenre` ("
    "      `idprogramme` int(10) unsigned NOT NULL,"
    "      `idgenre` int(10) unsigned NOT NULL,"
    "      KEY `idprogramme` (`idprogramme`),"
    "      KEY `idgenre` (`idgenre`),"
    "      KEY `idprogramme_2` (`idprogramme`),"
    "      KEY `idgenre_2` (`idgenre`)"
    "    );",

    ""
      };
      QString dbver = "";
      performActualUpdate(updates, "1000", dbver, MythiPlayerVersionName);

  }
}

  void UpgradeiPlayerDatabaseSchema()
  {
    QString dbver = gContext->GetSetting(MythiPlayerVersionName);

    if (dbver == currentDatabaseVersion)
        return;

    if (dbver == "")
    {
      InitializeProgrammeDatabase();
    }
  }
